package ru.tennisworld.match.services.match.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.tennisworld.match.dto.match.CreateMatchRequestDto;
import ru.tennisworld.match.dto.match.MatchFullResponseDto;
import ru.tennisworld.match.dto.match.UpdateMatchRequestDto;
import ru.tennisworld.match.entities.Match;
import ru.tennisworld.match.mappers.MatchMapper;
import ru.tennisworld.match.services.match.MatchControllerService;
import ru.tennisworld.match.services.match.MatchService;

@Service("matchControllerService")
public class MatchControllerServiceImpl implements MatchControllerService {

    @Autowired
    private MatchMapper matchMapper;

    @Autowired
    private MatchService matchService;

    private static final Logger LOG = LoggerFactory.getLogger(MatchControllerServiceImpl.class);

    @Override
    public MatchFullResponseDto getFullInfo(String matchId) {
        LOG.info("operation=MatchControllerServiceImpl.getFullInfo, action=start, matchId={}", matchId);
        Match match = matchService.getMatchById(Long.valueOf(matchId));
        MatchFullResponseDto result = matchMapper.mapToResponseDto(match);
        LOG.info("operation=MatchControllerServiceImpl.getFullInfo, action=end, result={}", result);
        return result;
    }

    @Override
    public MatchFullResponseDto createSingleMatch(CreateMatchRequestDto request) {
        LOG.info("operation=MatchControllerServiceImpl.createSingleMatch, action=start, request={}", request);
        Match match = matchMapper.matToEntity(request);
        match = matchService.createMatch(match);
        MatchFullResponseDto result = matchMapper.mapToResponseDto(match);
        LOG.info("operation=MatchControllerServiceImpl.createSingleMatch, action=end, result={}", result);
        return result;
    }

    @Override
    public MatchFullResponseDto updateSingleMatch(String matchId, UpdateMatchRequestDto request) {
        LOG.info("operation=MatchControllerServiceImpl.updateSingleMatch, action=start, request={}", request);
        Match src = matchService.getMatchById(Long.valueOf(matchId));
        Match upd = matchMapper.matToEntity(request);
        src = matchService.updateMatch(src, upd);
        matchService.updateUserRatingAfterMatch(src);
        MatchFullResponseDto result = matchMapper.mapToResponseDto(src);
        LOG.info("operation=MatchControllerServiceImpl.updateSingleMatch, action=end, result={}", result);
        return result;
    }
}
